package com.sunchen.asc.application.rest;

import com.alibaba.fastjson.JSONObject;
import com.sunchen.asc.service.common.ResultResponse;
import com.sunchen.asc.service.common.ResultUtil;
import com.sunchen.asc.service.user.api.UserService;
import com.sunchen.asc.service.user.dto.UserDto;
import com.sunchen.asc.service.user.vo.NewUserVo;
import com.sunchen.asc.service.user.vo.UserVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserRest {

    @DubboReference
    private UserService userService;

    @ApiOperation(value = "获取当前用户信息")
    @ApiImplicitParam(name="id",value = "用户id",required = true)
    @RequestMapping(value = "/current",method = RequestMethod.GET)
    public ResultResponse current(@RequestParam Long id){
        return userService.current(id);
    }

    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value = "姓名",dataType = "String",required = false),
            @ApiImplicitParam(name="userName",value = "登录名",dataType = "String",required = false),
            @ApiImplicitParam(name="page",value = "页码",dataType = "Integer",required = false,defaultValue = "1"),
            @ApiImplicitParam(name="limit",value = "每页显示条数",dataType = "Integer",required = false,defaultValue = "10"),
            @ApiImplicitParam(name="order",value = "排序字段",dataType = "String",required = false,defaultValue = "id"),
            @ApiImplicitParam(name="orderType",value = "升序/降序",dataType = "String",required = false,defaultValue = "asc")
    })
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public ResultResponse userList(@RequestParam(value = "name",required = false) String name,
                                   @RequestParam(value = "userName",required = false) String userName,
                                   @RequestParam(value = "page",defaultValue = "1",required = false) Integer page,
                                   @RequestParam(value = "limit",defaultValue = "10",required = false) Integer limit,
                                   @RequestParam(value = "order",defaultValue = "id",required = false) String order,
                                   @RequestParam(value = "orderType",defaultValue = "asc",required = false) String orderType){

        Sort sort = Sort.by(Sort.Direction.fromString(orderType),order);
        Pageable pageable = PageRequest.of(page-1,limit,sort);

        Page<UserVo> result = userService.getUserList(pageable,name,userName);
        return ResultUtil.success(result);
    }

    @ApiOperation(value = "用户详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",dataType = "long",required = true)
    })
    @RequestMapping(value = "/detail",method = RequestMethod.GET)
    public ResultResponse userDetail(@RequestParam Long id){
        return userService.userDetail(id);
    }

    @ApiOperation(value = "根据登录名获取用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "登陆名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    public ResultResponse getUser(@RequestParam String userName){
        return userService.getUser(userName);
    }

    @ApiOperation(value = "编辑用户")
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public ResultResponse editUser(@RequestBody @ApiParam(name="userVo对象",value="传入json格式",required=true) UserVo userVo){
        UserDto userDto = new UserDto(userVo);
        return userService.editUser(userDto);
    }

    @ApiOperation(value = "删除用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",dataType = "Long",required = true)
    })
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public ResultResponse deleteUser(@RequestParam Long id){

        return userService.deleteUser(id);
    }

    @ApiOperation(value = "用户重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",dataType = "Long",required = true)
    })
    @RequestMapping(value = "/password/reset",method = RequestMethod.GET)
    public ResultResponse resetPassword(@RequestParam Long id){

        return userService.resetPassword(id);
    }

    @ApiOperation(value = "用户启用/禁用")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",dataType = "Long",required = true),
            @ApiImplicitParam(name="status",value = "状态（1，2）",dataType = "Integer",required = true)
    })
    @RequestMapping(value = "/status/set",method = RequestMethod.GET)
    public ResultResponse setStatus(@RequestParam Long id,@RequestParam int status){

        return userService.setStatus(id,status);
    }

    @ApiOperation(value = "用户角色分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id",value = "用户id",required = true),
            @ApiImplicitParam(name="roleIds[]",value = "角色id集合",required = true)
    })
    @RequestMapping(value = "/role/set",method = RequestMethod.POST)
    public ResultResponse setUserRole(@RequestParam Long id,@RequestParam(value = "roleIds[]") List<Long> roleIds){
        System.out.println(id);
        System.out.println(roleIds);
        return userService.setUserRole(id,roleIds);
    }


    @ApiOperation(value = "表单验证创建时登录名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "登陆名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/onlyname/create",method = RequestMethod.GET)
    public JSONObject findTheOnlyUserNameCreat(@RequestParam String userName){

        return userService.findTheOnlyUserNameCreat(userName);
    }

    @ApiOperation(value = "表单验证修改时登录名是否唯一")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userName",value = "登陆名",dataType = "String",required = true)
    })
    @RequestMapping(value = "/onlyname/modify",method = RequestMethod.GET)
    public JSONObject findTheOnlyUserNameModify(@RequestParam String userName,@RequestParam Long id){

        return userService.findTheOnlyUserNameModify(userName,id);
    }

}
